local CreateFrame = CreateFrame
local Talented = Talented
local SHINE_SPEED = 2.5

local shines = Talented.Pool:new()

local function CreateTexture(parent)
	local t = parent:CreateTexture(nil, "BACKGROUND")
	t:SetTexture("Interface\\ItemSocketingFrame\\UI-ItemSockets")
	t:SetBlendMode("ADD")
	t:SetWidth(13)
	t:SetHeight(13)
	t:SetTexCoord(0.3984375, 0.4453125, 0.40234375, 0.44921875)
	t:SetPoint("CENTER", parent)
	return t
end

local function Shine_Start(self)
end

local function Shine_OnHide(self)
	local textures = self.textures
	for i = 1, 4 do
		textures[i]:SetVertexColor(1, 1, 1)
	end
end

local function Shine_OnUpdate(self, elapsed)
	local timer = self.timer + elapsed
	while timer >= SHINE_SPEED do
		timer = timer - SHINE_SPEED
	end
	self.timer = timer
	local distance = self:GetWidth() * timer / SHINE_SPEED
	local textures = self.textures
	textures[1]:SetPoint("CENTER", self, "TOPLEFT", distance, 0)
	textures[2]:SetPoint("CENTER", self, "BOTTOMRIGHT", -distance, 0)
	textures[3]:SetPoint("CENTER", self, "TOPRIGHT", 0, -distance)
	textures[4]:SetPoint("CENTER", self, "BOTTOMLEFT", 0, distance)
end

local function NewShine(parent)
	local shine = CreateFrame("Frame", nil, parent)
	local textures = {}
	for i = 1, 4 do
		textures[i] = CreateTexture(shine)
	end
	shine.textures = textures
	shine:SetScript("OnUpdate", Shine_OnUpdate)
	shine:SetScript("OnHide", Shine_OnHide)
	
	return shine
end

function Talented:MakeShine(parent, r, g, b)
	local shine = shines:next()
	if shine then
		shine:SetParent(parent)
	else
		shine = NewShine(parent)
	end
	local textures = shine.textures
	for i = 1, 4 do
		textures[i]:SetVertexColor(r, g, b)
	end
	shine.timer = 0
	shine:Show()
	shine:SetAllPoints()
	shine:SetFrameLevel(parent:GetFrameLevel() + 5)
	shines:push(shine)
	return shine
end

function Talented:ClearAllShines()
	--hackish
	rawset(shines, "count", 0)
	for _, widget in ipairs(shines.pool) do
		widget:Hide()
	end
end
